<template>
  <div>
    <!-- 面包屑导航 -->
    <el-breadcrumb separator="/">
      <el-breadcrumb-item :to="{ path: '/index' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>基础数据管理</el-breadcrumb-item>
      <el-breadcrumb-item>申请信息管理</el-breadcrumb-item>
    </el-breadcrumb>

    <el-row :gutter="20">
      <!-- 左侧项目表格卡片 -->
      <el-col :span="16">
        <el-card>
          <div slot="header" class="card-header">
            <div>
              <el-form :inline="true" :model="find">
                <el-form-item label="项目id">
                  <el-input
                    v-model="find.id"
                    placeholder="请输入项目id"
                  ></el-input>
                </el-form-item>
                <el-form-item>
                  <el-button type="primary">查询</el-button>
                </el-form-item>
              </el-form>
            </div>
            <div>
              <el-button type="primary" @click="addProjectClick"
                >添加项目</el-button
              >
            </div>
          </div>
          <!-- 卡片主体 -->
          <div class="card-body">
            <el-tabs>
              <!-- 进行中项目 -->
              <el-tab-pane label="进行中项目">
                <el-table
                  height="376px"
                  :data="
                    aliveProject.filter(
                      (data) =>
                        !search ||
                        data.id.toLowerCase().includes(search.toLowerCase()) ||
                        data.name
                          .toLowerCase()
                          .includes(search.toLowerCase()) ||
                        data.salary.toLowerCase().includes(search.toLowerCase())
                    )
                  "
                  style="width: 100%"
                  border
                >
                  <el-table-column prop="id" label="项目id" width="120">
                  </el-table-column>
                  <el-table-column prop="name" label="项目名">
                  </el-table-column>
                  <el-table-column prop="salary" label="资金" width="100">
                  </el-table-column>
                  <el-table-column prop="jobNum" label="岗位数量" width="100">
                  </el-table-column>
                  <el-table-column
                    prop="underJobNum"
                    label="申请添加岗位"
                    width="110"
                  >
                  </el-table-column>
                  <el-table-column align="center" width="200">
                    <template slot="header" slot-scope="{}">
                      <el-input
                        v-model="search"
                        size="mini"
                        placeholder="输入关键字搜索"
                      />
                    </template>
                    <template slot-scope="scope">
                      <el-button @click="ProjectDetail(scope.row)" type="text">
                        详情
                      </el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
              <!-- 全部项目 -->
              <el-tab-pane label="全部项目">
                <el-table
                  height="376px"
                  :data="
                    allProject.filter(
                      (data) =>
                        !search ||
                        data.id.toLowerCase().includes(search.toLowerCase()) ||
                        data.name
                          .toLowerCase()
                          .includes(search.toLowerCase()) ||
                        data.salary.toLowerCase().includes(search.toLowerCase())
                    )
                  "
                  style="width: 100%"
                  border
                >
                  <el-table-column prop="id" label="项目id" width="120">
                  </el-table-column>
                  <el-table-column prop="name" label="项目名">
                  </el-table-column>
                  <el-table-column prop="salary" label="资金" width="100">
                  </el-table-column>
                  <el-table-column prop="jobNum" label="岗位数量" width="100">
                  </el-table-column>
                  <el-table-column align="center" width="200">
                    <template slot="header" slot-scope="{}">
                      <el-input
                        v-model="search"
                        size="mini"
                        placeholder="输入关键字搜索"
                      />
                    </template>
                    <template slot-scope="scope">
                      <el-button @click="ProjectDetail(scope.row)" type="text">
                        详情
                      </el-button>
                    </template>
                  </el-table-column>
                </el-table>
              </el-tab-pane>
            </el-tabs>
          </div>
          <!-- 分页 -->
          <div class="block table-footer">
            <paging :PageData="page"></paging>
          </div>
        </el-card>
      </el-col>
      <!-- 右侧资金申请中项目卡片 -->
      <el-col :span="8">
        <el-card>
          <div slot="header" class="card-header">
            <h3>资金申请中项目</h3>
          </div>
          <div
            class="card-body aligning"
            v-for="item in underProject"
            :key="item.id"
          >
            <el-row :gutter="20">
              <el-col :span="18">
                <div class="c-center-text">
                  <span>id: {{ item.id }}</span>
                  <span>项目名: {{ item.name }}</span>
                </div>
              </el-col>
              <el-col :span="6">
                <el-button type="text" @click="ProjectDetail(item)">
                  查看详情
                </el-button>
              </el-col>
            </el-row>
            <el-divider></el-divider>
          </div>
        </el-card>
      </el-col>
    </el-row>

    <!-- 添加按钮弹窗 -->
    <el-dialog title="添加项目" :visible.sync="dialogAddVisible">
      <el-form :model="addProject">
        <el-form-item label="项目名" :label-width="formLabelWidth">
          <el-input v-model="addProject.name"></el-input>
        </el-form-item>
        <el-form-item label="资金配额" :label-width="formLabelWidth">
          <el-input-number
            :precision="2"
            :max="maxSalary"
            :step="0.01"
            v-model="addProject.salary"
          ></el-input-number>
        </el-form-item>
        <el-form-item label="项目时间" :label-width="formLabelWidth">
          <div class="block">
            <el-date-picker
              v-model="addProject.date"
              value-format="yyyy-MM-dd"
              type="daterange"
              align="right"
              unlink-panels
              range-separator="至"
              start-placeholder="开始日期"
              end-placeholder="结束日期"
            >
            </el-date-picker>
          </div>
        </el-form-item>
        <el-form-item label="描述" :label-width="formLabelWidth">
          <el-input
            :rows="5"
            type="textarea"
            v-model="addProject.description"
          ></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogAddVisible = false">取 消</el-button>
        <el-button type="primary" @click="addProjectYes">送 审</el-button>
      </div>
    </el-dialog>

    <!-- 详情按钮弹窗 -->
    <el-dialog title="项目详情" :visible.sync="dialogDetailVisible">
      <!-- 项目信息 -->
      <el-descriptions title="项目信息" border>
        <el-descriptions-item label="项目id">
          {{ DetailProject.baseinfo.id }}
        </el-descriptions-item>
        <el-descriptions-item label="项目名">
          {{ DetailProject.baseinfo.name }}
        </el-descriptions-item>
        <el-descriptions-item label="资金">
          {{ DetailProject.baseinfo.salary }}
        </el-descriptions-item>
        <el-descriptions-item label="起止日期">
          {{ DetailProject.baseinfo.startDate }} 至
          {{ DetailProject.baseinfo.endDate }}
        </el-descriptions-item>
        <el-descriptions-item
          label="岗位数量"
          v-if="DetailProject.baseinfo.jobNum !== undefined"
        >
          {{ DetailProject.baseinfo.jobNum }}
        </el-descriptions-item>
        <el-descriptions-item
          label="申请中岗位数量"
          v-if="DetailProject.baseinfo.underJobNum !== undefined"
        >
          <span
            v-for="(item, index) in DetailProject.baseinfo.underJobNum"
            :key="index"
          >
            {{ DetailProject.baseinfo.underJobNum }}
          </span>
        </el-descriptions-item>
      </el-descriptions>

      <template v-if="DetailProject.baseinfo.status !== '0'">
        <!-- 分割线 -->
        <el-divider></el-divider>

        <!-- 项目岗位 -->
        <h3>项目岗位列表</h3>
        <el-table
          title="项目岗位列表"
          :data="DetailProject.jobs"
          border
          max-height="28rem"
        >
          <el-table-column prop="id" label="岗位id"></el-table-column>
          <el-table-column prop="name" label="岗位名称"></el-table-column>
          <el-table-column prop="salary" label="岗位薪资"></el-table-column>
          <el-table-column prop="status" label="岗位状态"></el-table-column>
        </el-table>
        <Paging :PageData="DetailProject.jobPage"></Paging>
      </template>

      <template v-else>
        <!-- 同意/拒绝按钮 -->
        <span slot="footer" class="dialog-footer">
          <el-button type="primary" @click="dialogDetailVisible = false">
            返回
          </el-button>
          <el-button type="danger" @click="applyCancel"> 取消申请 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import Paging from "../components/Paging.vue";
import { getPagList } from "../api/project";
import { getPagListLive } from "../api/project";

export default {
  name: "ProjectControl",
  components: {
    Paging,
  },
  data() {
    return {
      aliveProject: [
        {
          id: "1",
          name: "图书馆",
          salary: "8000",
          jobNum: "10",
          underJobNum: "1",
          startDate: "2021-09-01",
          endDate: "2021-10-01",
        },
        {
          id: "2",
          name: "教学楼",
          salary: "8000",
          jobNum: "8",
          underJobNum: "4",
          startDate: "2021-09-01",
          endDate: "2021-10-01",
        },
        {
          id: "3",
          name: "资料",
          salary: "7000",
          jobNum: "11",
          underJobNum: "0",
          startDate: "2021-09-01",
          endDate: "2021-10-01",
        },
      ],
      allProject: [
        {
          id: "1",
          name: "图书馆",
          salary: "8000",
          jobNum: "11",
          startDate: "2021-09-01",
          endDate: "2021-10-01",
        },
        {
          id: "2",
          name: "教学楼",
          salary: "8000",
          jobNum: "11",
          startDate: "2021-09-01",
          endDate: "2021-10-01",
        },
        {
          id: "3",
          name: "资料",
          salary: "7000",
          jobNum: "11",
          startDate: "2021-09-01",
          endDate: "2021-10-01",
        },
        {
          id: "4",
          name: "操场",
          salary: "8000",
          jobNum: "11",
          startDate: "2021-09-01",
          endDate: "2021-10-01",
        },
        {
          id: "5",
          name: "食堂",
          salary: "10000",
          jobNum: "11",
          startDate: "2021-09-01",
          endDate: "2021-10-01",
        },
      ],
      underProject: [
        {
          id: "4",
          name: "操场",
          salary: "8000",
          status: "0",
          startDate: "2021-09-01",
          endDate: "2021-10-01",
        },
      ],
      page: {
        pageSizes: [5, 10, 50, 100],
        pageSize: 5,
        currentPage: 1,
        total: 0,
      },
      page1: {
        pageSizes: [5, 10, 50, 100],
        pageSize: 5,
        currentPage: 1,
        total: 0,
      },
      page2: {
        pageSizes: [5, 10, 50, 100],
        pageSize: 5,
        currentPage: 1,
        total: 0,
      },
      maxSalary: 20000,
      currentPage: 1,
      search: "",
      find: {
        id: "",
      },
      dialogAddVisible: false,
      dialogDetailVisible: false,
      DetailProject: {
        baseinfo: {},
        jobs: [],
        jobPage: {
          pageSizes: [5, 10, 50, 100],
          pageSize: 5,
          currentPage: 1,
          total: 0,
        },
      },
      addProject: {
        name: "",
        salary: 0,
        description: "",
        date: [], // 0: 开始时间   1: 结束时间
      },
      formLabelWidth: "120px",
    };
  },
  methods: {
    ProjectDetail(row) {
      this.dialogDetailVisible = true;
      this.DetailProject.baseinfo = row;
    },
    addProjectClick() {
      this.dialogAddVisible = true;
      console.log(this.addProject.date);
    },
    addProjectYes() {
      console.log(this.addProject);
    },
  },
  created() {
    getPagList({
      pageSize: this.page.pageSize,
      currentPage: this.page.currentPage,
    }).then((res) => {
      this.allProject = res.data.data.data;
      this.page2.total = res.data.data.total;
      this.page2.currentPage = res.data.data.currentPage;
      this.page = this.page2;
    });

    getPagListLive({
      pageSize: this.page.pageSize,
      currentPage: this.page.currentPage,
    }).then((res) => {
      this.aliveProject = res.data.data.data;
      this.page1.total = res.data.data.total;
      this.page1.currentPage = res.data.data.currentPage;
      this.page = this.page1;
    });
  },
};
</script>

<style lang="less" scoped>
.el-card {
  .card-header {
    display: flex;
    justify-content: space-between;
  }

  .card-body {
    min-height: 28rem;
  }

  .el-row {
    margin-bottom: 0px;
  }
}
.c-center-text {
  display: flex;
  flex-direction: column;
  justify-content: center;
}
.aligning {
  min-height: 30rem !important;
}

.el-dialog {
  text-align: center;
  .el-form {
    .el-form-item {
      margin-right: 60px;
      * {
        width: 100%;
      }
    }
  }
}
</style>